<template>
  <div class="dialog">
    <el-dialog width="45%" :title="title" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="类目名称：" prop="name">
          <el-col :span="12">
            <el-input v-model="form.name" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <template v-if="!id">
          <el-form-item label="行业：" prop="industryCode">
            <el-col :span="12">
              <el-select v-model="form.industryCode" placeholder="请选择" clearable @change="getCategoryTree">
                <el-option
                  v-for="item in industryList"
                  :key="item.industryCode"
                  :label="item.industryName"
                  :value="item.industryCode"
                />
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="上级类目：" prop="parentId">
            <el-col :span="12">
              <el-cascader
                v-model="form.parentId"
                :options="categoryTree"
                :props="{label: 'name', value: 'id', children: 'categoryTreeVOS',checkStrictly: true}"
                clearable
              />
            </el-col>
          </el-form-item>
        </template>
        <el-form-item label="排序：" prop="sort">
          <el-col :span="12">
            <el-input v-model="form.sort" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getIndustryList,
  addCategory,
  editCategory,
  getCategoryDetail,
  getCategoryTree
} from '@/api/dictionary'
export default {
  name: 'AddCategory',
  props: {
    id: {
      type: [Number, String],
      default: ''
    }
  },

  data() {
    return {
      loading: false,
      industryList: [],
      categoryTree: [],
      form: {
        name: '',
        industryCode: '',
        parentId: [],
        sort: ''
      },
      rules: {
        name: [{ required: true, message: '请输入类目名称', trigger: ['blur'] }],
        industryCode: [{ required: true, message: '请选择行业', trigger: ['change'] }]
      }
    }
  },

  computed: {
    title() {
      if (this.id) {
        return '编辑类目'
      } else {
        return '新增类目'
      }
    }
  },

  created() {
    this.getIndustryList()
    if (this.id) {
      this.getDetail()
    }
  },

  methods: {

    // 获取详情
    getDetail() {
      const data = {
        id: this.id
      }
      getCategoryDetail(data).then(res => {
        const { name, sort } = res
        this.form.name = name
        this.form.sort = sort
      })
    },

    // 获取行业列表
    getIndustryList() {
      getIndustryList().then(res => {
        this.industryList = res || []
      }).catch(() => {})
    },

    // 根据行业获取类目树
    getCategoryTree(industryCode) {
      const data = {
        industryCode
      }
      getCategoryTree(data).then(res => {
        this.categoryTree = res || []
      }).catch(() => {})
    },

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      if (this.id) {
        this.editHandle()
      } else {
        this.addHandle()
      }
    },

    // 新增
    addHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const parentId = this.form.parentId[this.form.parentId?.length - 1]
          const data = {
            ...this.form,
            parentId
          }
          addCategory(data).then(() => {
            this.$message.success('新增成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },

    // 编辑
    editHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form,
            id: this.id
          }
          editCategory(data).then(() => {
            this.$message.success('编辑成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select,.el-cascader{
  width: 100%;
}
</style>
